package com.weishaoying.example.security.service;

import com.weishaoying.example.security.model.SysUser;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WeiShaoying
 * @date 2018/4/3
 */
public class CustomerUserService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //模拟通过DAO查询获得User
        SysUser user = new SysUser(8,"Jack","p123","root");
        List<SimpleGrantedAuthority> grantedAuthorityList = new ArrayList<>();
        grantedAuthorityList.add(new SimpleGrantedAuthority(user.getRole()));
        //返回的是三要素：用户名、密码、授权列表
        return new User(user.getUsername(), user.getPassword(), grantedAuthorityList);
    }
}
